/*!
 * Copyright (c) Microsoft Corporation and contributors. All rights reserved.
 * Licensed under the MIT License.
 */

import { IRandom, makeRandom } from "@fluid-private/stochastic-test-utils";
import { InsertableTypedNode } from "@fluidframework/tree";

import { Row } from "./tree/index.js";

export function generateRow(random: IRandom): InsertableTypedNode<typeof Row> {
	const [itemType, unitPrice, unitCost] = random.pick([
		["Vegetables", 154.06, 90.93],
		["Beverages", 47.45, 31.79],
		["Office Supplies", 651.21, 524.96],
		["Meat", 421.89, 364.69],
		["Personal Care", 81.73, 56.67],
		["Clothes", 109.28, 35.84],
		["Household", 668.27, 502.54],
		["Cereal", 205.7, 117.11],
		["Cosmetics", 437.2, 263.33],
		["Snacks", 152.58, 97.44],
		["Fruits", 9.33, 6.92],
		["Baby Food", 255.28, 159.42],
	]);

	const toDollars = (n: number) => Math.round(n * 100) / 100;

	const unitsSold = random.integer(2, 10000);
	const totalRevenue = toDollars(unitPrice * unitsSold);
	const totalCost = toDollars(unitCost * unitsSold);
	const totalProfit = toDollars(totalRevenue - totalCost);
	const orderDate = random.integer(1262332800000, 1501225200000);
	const shippingDate = orderDate + random.integer(0, 4323600000);

	const [country, region] = random.pick([
		["Montenegro", "Europe"],
		["The Bahamas", "Central America and the Caribbean"],
		["Norway", "Europe"],
		["Togo", "Sub-Saharan Africa"],
		["Saint Kitts and Nevis ", "Central America and the Caribbean"],
		["Vanuatu", "Australia and Oceania"],
		["United States of America", "North America"],
		["Libya", "Middle East and North Africa"],
		["Kazakhstan", "Asia"],
		["Guinea", "Sub-Saharan Africa"],
		["Malaysia", "Asia"],
		["Republic of the Congo", "Sub-Saharan Africa"],
		["Honduras", "Central America and the Caribbean"],
		["Antigua and Barbuda ", "Central America and the Caribbean"],
		["Democratic Republic of the Congo", "Sub-Saharan Africa"],
		["Bosnia and Herzegovina", "Europe"],
		["Bahrain", "Middle East and North Africa"],
		["South Africa", "Sub-Saharan Africa"],
		["Botswana", "Sub-Saharan Africa"],
		["Palau", "Australia and Oceania"],
		["Pakistan", "Middle East and North Africa"],
		["Seychelles ", "Sub-Saharan Africa"],
		["Luxembourg", "Europe"],
		["Myanmar", "Asia"],
		["Nicaragua", "Central America and the Caribbean"],
		["Indonesia", "Asia"],
		["Taiwan", "Asia"],
		["Sao Tome and Principe", "Sub-Saharan Africa"],
		["New Zealand", "Australia and Oceania"],
		["Angola", "Sub-Saharan Africa"],
		["North Korea", "Asia"],
		["Austria", "Europe"],
		["Armenia", "Europe"],
		["Croatia", "Europe"],
		["Nepal", "Asia"],
		["Portugal", "Europe"],
		["Serbia", "Europe"],
		["Spain", "Europe"],
		["Ireland", "Europe"],
		["Kiribati", "Australia and Oceania"],
		["Czech Republic", "Europe"],
		["Moldova ", "Europe"],
		["Albania", "Europe"],
		["Kenya", "Sub-Saharan Africa"],
		["Saint Vincent and the Grenadines", "Central America and the Caribbean"],
		["Russia", "Europe"],
		["Greece", "Europe"],
		["Panama", "Central America and the Caribbean"],
		["Guatemala", "Central America and the Caribbean"],
		["Macedonia", "Europe"],
		["El Salvador", "Central America and the Caribbean"],
		["Malawi", "Sub-Saharan Africa"],
		["Solomon Islands", "Australia and Oceania"],
		["Monaco", "Europe"],
		["Japan", "Asia"],
		["Jordan", "Middle East and North Africa"],
		["Comoros", "Sub-Saharan Africa"],
		["Vietnam", "Asia"],
		["Djibouti", "Sub-Saharan Africa"],
		["Egypt", "Middle East and North Africa"],
		["South Korea", "Asia"],
		["Zambia", "Sub-Saharan Africa"],
		["Italy", "Europe"],
		["Maldives", "Asia"],
		["Georgia", "Europe"],
		["Ghana", "Sub-Saharan Africa"],
		["Tanzania", "Sub-Saharan Africa"],
		["Fiji", "Australia and Oceania"],
		["Nigeria", "Sub-Saharan Africa"],
		["Bhutan", "Asia"],
		["Trinidad and Tobago", "Central America and the Caribbean"],
		["Rwanda", "Sub-Saharan Africa"],
		["Denmark", "Europe"],
		["Nauru", "Australia and Oceania"],
		["Iraq", "Middle East and North Africa"],
		["Tunisia ", "Middle East and North Africa"],
		["Cape Verde", "Sub-Saharan Africa"],
		["Uzbekistan", "Asia"],
		["Poland", "Europe"],
		["Iceland", "Europe"],
		["Swaziland", "Sub-Saharan Africa"],
		["Zimbabwe", "Sub-Saharan Africa"],
		["Liberia", "Sub-Saharan Africa"],
		["Afghanistan", "Middle East and North Africa"],
		["Cambodia", "Asia"],
		["Turkey", "Middle East and North Africa"],
		["Vatican City", "Europe"],
		["Sierra Leone", "Sub-Saharan Africa"],
		["Hungary", "Europe"],
		["Central African Republic", "Sub-Saharan Africa"],
		["Thailand", "Asia"],
		["Jamaica", "Central America and the Caribbean"],
		["China", "Asia"],
		["Bangladesh", "Asia"],
		["Burundi", "Sub-Saharan Africa"],
		["Yemen", "Middle East and North Africa"],
		["Grenada", "Central America and the Caribbean"],
		["India", "Asia"],
		["Kuwait", "Middle East and North Africa"],
		["Morocco", "Middle East and North Africa"],
		["Singapore", "Asia"],
		["Switzerland", "Europe"],
		["Lesotho", "Sub-Saharan Africa"],
		["United Arab Emirates", "Middle East and North Africa"],
		["Chad", "Sub-Saharan Africa"],
		["Mozambique", "Sub-Saharan Africa"],
		["Uganda", "Sub-Saharan Africa"],
		["Oman", "Middle East and North Africa"],
		["East Timor", "Australia and Oceania"],
		["Algeria", "Middle East and North Africa"],
		["Latvia", "Europe"],
		["Haiti", "Central America and the Caribbean"],
		["Madagascar", "Sub-Saharan Africa"],
		["Mauritius ", "Sub-Saharan Africa"],
		["Niger", "Sub-Saharan Africa"],
		["South Sudan", "Sub-Saharan Africa"],
		["United Kingdom", "Europe"],
		["Sri Lanka", "Asia"],
		["Lebanon", "Middle East and North Africa"],
		["Andorra", "Europe"],
		["Belgium", "Europe"],
		["Sudan", "Sub-Saharan Africa"],
		["Saudi Arabia", "Middle East and North Africa"],
		["Estonia", "Europe"],
		["Israel", "Middle East and North Africa"],
		["Tajikistan", "Asia"],
		["Saint Lucia", "Central America and the Caribbean"],
		["Dominican Republic", "Central America and the Caribbean"],
		["Benin", "Sub-Saharan Africa"],
		["Belize", "Central America and the Caribbean"],
		["Laos", "Asia"],
		["Slovenia", "Europe"],
		["Sweden", "Europe"],
		["Barbados", "Central America and the Caribbean"],
		["Liechtenstein", "Europe"],
		["Ukraine", "Europe"],
		["Federated States of Micronesia", "Australia and Oceania"],
		["Samoa ", "Australia and Oceania"],
		["Mongolia", "Asia"],
		["The Gambia", "Sub-Saharan Africa"],
		["Cote d'Ivoire", "Sub-Saharan Africa"],
		["Burkina Faso", "Sub-Saharan Africa"],
		["Kosovo", "Europe"],
		["Guinea-Bissau", "Sub-Saharan Africa"],
		["Australia", "Australia and Oceania"],
		["Belarus", "Europe"],
		["Somalia", "Middle East and North Africa"],
		["Mauritania", "Sub-Saharan Africa"],
		["Cyprus", "Europe"],
		["Philippines", "Asia"],
		["Namibia", "Sub-Saharan Africa"],
		["Canada", "North America"],
		["Kyrgyzstan", "Asia"],
		["Marshall Islands", "Australia and Oceania"],
		["Senegal", "Sub-Saharan Africa"],
		["France", "Europe"],
		["Mali", "Sub-Saharan Africa"],
		["Papua New Guinea", "Australia and Oceania"],
		["Lithuania", "Europe"],
		["Germany", "Europe"],
		["Ethiopia", "Sub-Saharan Africa"],
		["Equatorial Guinea", "Sub-Saharan Africa"],
		["Syria", "Middle East and North Africa"],
		["Eritrea", "Sub-Saharan Africa"],
		["Cuba", "Central America and the Caribbean"],
		["Tonga", "Australia and Oceania"],
		["Finland", "Europe"],
		["Gabon", "Sub-Saharan Africa"],
		["Tuvalu", "Australia and Oceania"],
		["Qatar", "Middle East and North Africa"],
		["Slovakia", "Europe"],
		["Turkmenistan", "Asia"],
		["Greenland", "North America"],
		["Dominica", "Central America and the Caribbean"],
		["Azerbaijan", "Middle East and North Africa"],
		["Cameroon", "Sub-Saharan Africa"],
		["Netherlands", "Europe"],
		["Iran", "Middle East and North Africa"],
		["Costa Rica", "Central America and the Caribbean"],
		["Bulgaria", "Europe"],
		["Romania", "Europe"],
		["Mexico", "North America"],
		["Malta", "Europe"],
		["Brunei", "Asia"],
		["San Marino", "Europe"],
	]);

	return {
		"Order ID": random.integer(100000000, 999999999),
		"Region": region,
		"Country": country,
		"Item Type": itemType,
		"Sales Channel": random.pick(["Online", "Offline"]),
		"Order Priority": random.pick(["H", "M", "L", "C"]),
		"Units Sold": unitsSold,
		"Unit Price": unitPrice,
		"Unit Cost": unitCost,
		"Total Revenue": totalRevenue,
		"Total Cost": totalCost,
		"Total Profit": totalProfit,
		"Order Date": orderDate,
		"Ship Date": shippingDate,
	};
}

export function generateTable(rows: number, seed = 1) {
	const random = makeRandom(seed);

	return Array.from({ length: rows }, () => generateRow(random)).sort(
		(left, right) => left["Order ID"] - right["Order ID"],
	);
}
